package com.gxuwz.controller;

import com.gxuwz.entity.Instructor;
import com.gxuwz.entity.Student;
import com.gxuwz.entity.User;
import com.gxuwz.result.Code;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.LoginService;
import com.gxuwz.util.CheckCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:登录功能模块
 */

@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * @Description:实现根据角色进行登录功能，并跳转至首页
     * @Param:  [java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @PostMapping
    public ModelAndView loginUser(String username,String password, String identity,String checkCode, HttpServletRequest request,ModelAndView modelAndView){
        Map<String,Object> userInfo = new HashMap<String, Object>();
        userInfo.put("username",username);
        userInfo.put("password",password);
        userInfo.put("identity",identity);
        Object user = loginService.checkUser(userInfo);
        String message = user != null ? "登录成功" : "账号或密码错误";
        HttpSession session = request.getSession();
        if(!checkCode.equals(session.getAttribute("checkCodeGen"))){
            ResultInfo resultInfo = new ResultInfo(Code.GET_ERR, "验证码错误");
            modelAndView.addObject("resultInfo",resultInfo);
            modelAndView.setViewName("login");
            return modelAndView;
        }
        if(user != null){
            if(identity.equals("系统管理员")){
                User userObject = (User) user;
                session.setAttribute("username",userObject.getUserId());
                session.setAttribute("telephone",userObject.getTelephone());
                session.setAttribute("fullName",userObject.getFullName());
                session.setAttribute("identity",identity);
            } else if(identity.equals("辅导员")){
                Instructor instructor = (Instructor) user;
                session.setAttribute("username",instructor.getInstID());
                session.setAttribute("telephone",instructor.getTelephone());
                session.setAttribute("fullName",instructor.getInstName());
                session.setAttribute("identity",identity);
            } else {
                Student student = (Student) user;
                session.setAttribute("username",student.getStuID());
                session.setAttribute("telephone",student.getStuTel());
                session.setAttribute("fullName",student.getStuName());
                session.setAttribute("identity",identity);
            }
            modelAndView.addObject("resultInfo",new ResultInfo(Code.GET_OK,message));
            modelAndView.setViewName("home");
            return modelAndView;
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.GET_ERR,message));
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }

    /**
     * @Description:获取验证码，并将验证码存入session
     * @Param:  [javax.servlet.http.HttpServletResponse, javax.servlet.http.HttpServletRequest]
     * @Return: void
     */

    @GetMapping("/checkCode")
    public void checkCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("image/jpeg");
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, os, 4);
        HttpSession session = request.getSession();
        session.setAttribute("checkCodeGen",checkCode);
    }


}
